<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * Banner管理
 */
class BannerController extends AdminBasicController {
    public $banner = '';
    public function _initialize(){
        $this->checkLogin();
        $this->banner = D('Banner');
    }

    /**
     * banner列表
     * @author mss
     * @time 2018-04-11
     */
    public function bannerList(){
        $w['status'] = array('NEQ',9);
        if(!empty(I('request.start_time'))&&!empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $parameter['start_time'] = I('request.start_time');
            $parameter['end_time'] = I('request.end_time');
        }
        $list = $this->banner->selectBanner($w,'sort DESC,b_id DESC',15,$parameter);
        $this->assign('list',$list['list']);

        $this->assign('page',$list['page']);
        $this->display('bannerList');
    }

    /**
     * 添加banner
     * @author mss
     * @time 2018-04-11
     */
    public function addBanner(){
        if(IS_POST){
            $data = $this->banner->create();
            if($data){
                $pics = $_POST['pic'];
                foreach($pics as $k=>$v){
                    $data = array();
                    $data['pic'] = $v;
                    $data['ctime'] = time();
                    $this->banner->addBanner($data);
                }
                $this->success('添加成功',U('Banner/bannerList'));
            }else{
                $this->error($this->banner->getError());
            }
        }else{

            $this->display('addBanner');
        }
    }

    /**
     * 更改图片显示状态
     * @author mss
     * @time 2017-12-14
     */
    public function ajaxStatus(){
        $pic_id = $_POST['id'];
        $status = $_POST['status'];
        $w['b_id'] = $pic_id;
        $data['status'] = $status;
        $data['utime'] = time();
        $res = $this->banner->editBanner($w,$data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error("修改失败");
        }
    }

    /**
     * 更改图片显示顺序
     * @author mss
     * @time 2017-12-19
     */
    public function ajaxSort(){
        $pic_id = $_POST['b_id'];
        $sort = $_POST['sort'];
        $w['b_id'] = $pic_id;
        $data['sort'] = $sort;
        $data['utime'] = time();
        $res = M('Banner')->where($w)->limit(1)->save($data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }


    /**ajax上传图片*/
    public function uploadPic(){
        $pic       = $_POST['pic'];
        $pic_name      = $_POST['pic_name'];
        $temp = explode('.',$pic_name);
        $ext = uniqid().'.'.end($temp);
        $base64    = substr(strstr($pic, ","), 1);
        $image_res = base64_decode($base64);
        $pic_link  = "Uploads/Banner/".date('Y-m-d').'/'.$ext;
        $saveRoot = "Uploads/Banner/".date('Y-m-d').'/';
        /**检查目录是否存在  循环创建目录*/
        if(!is_dir($saveRoot)){
            mkdir($saveRoot, 0777, true);
        }
        $res = file_put_contents($pic_link ,$image_res);
        if($res){
                $ajaxData = array("flag" => "success", "message"=>"上传成功！" );
                $result_data['path'] = $pic_link;
                $ajaxData['data'] = $result_data;
                $this->ajaxReturn(json_encode($ajaxData));
        }else{
            $ajaxData = array("flag" => "error", "message"=>"上传失败","data" => array());
            $this->ajaxReturn(json_encode($ajaxData));
        }
    }

    /**删除上传的相册的图片*/
    public function delPhoto(){
        $pic = "";

        $file  = $_POST['file_path'];
        $result = @unlink($file);
        if ($result == false && empty($pic)) {
            $this->ajaxReturn(0);
        } else {
            $this->ajaxReturn(1);
        }
    }

    /**
     * 删除图片
     * @author mss
     * @time 2017-12-14
     */
    public function delBanner(){
        if(empty($_REQUEST['b_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['b_id'] = array('IN',I('request.b_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->banner->editBanner($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

}